﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETPerformance.App_BusinessLogic.BasePages;
using ASPNETPerformance.App_BusinessLogic.Helpers;
using ASPNETPerformance.App_BusinessLogic.Image;
using ASPNETPerformance.App_BusinessLogic.JsAndCss;
using ASPNETPerformance.App_BusinessLogic.Utils;

namespace ASPNETPerformance.UserControls {
    public partial class Part3 : BaseUserControl {
        protected void Page_Load( object sender, EventArgs e ) {
            if ( !this.Page.IsPostBack ) {
                BindPart3();
            }
        }

        private void BindPart3() {
            this.txtProductDetailHeight.Value = CommonHelper.GetCookieInt( "ProductDetailHeight" );
            this.txtProductDetailWidth.Value = CommonHelper.GetCookieInt( "ProductDetailWidth" );

            this.txtShoppingCartItemHeight.Value = CommonHelper.GetCookieInt( "ShoppingCartItemHeight" );
            this.txtShoppingCartItemWidth.Value = CommonHelper.GetCookieInt( "ShoppingCartItemWidth" );

            this.cbEnableStaticCacheByImage.Checked = CommonHelper.GetCookieBool( "IsStaticCacheByImage" );

            this.cbEnableBustImageByCss.Checked = CommonHelper.GetCookieBool( "IsBustImageByCss" );

            this.cbEnabelImageBase64String.Checked = CommonHelper.GetCookieBool( "IsImageBase64String" );

            this.cbEnabelImageBase64String.Checked = CommonHelper.GetCookieBool( "IsSpriteImageOptimizationModule" );
        }

        protected void BtnPart3_Click( object sender, EventArgs e ) {
            SaveInfo();

            this.lblPart3Result.Text = "保存成功。";
        }

        public void SaveInfo() {
            CommonHelper.SetCookie( "ProductDetailHeight",
           this.txtProductDetailHeight.Value.ToString().Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );
            CommonHelper.SetCookie( "ProductDetailWidth",
                this.txtProductDetailWidth.Value.ToString().Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );

            CommonHelper.SetCookie( "ShoppingCartItemHeight",
                this.txtShoppingCartItemHeight.Value.ToString().Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );
            CommonHelper.SetCookie( "ShoppingCartItemWidth",
                this.txtShoppingCartItemWidth.Value.ToString().Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );

            CommonHelper.SetCookie( "IsStaticCacheByImage",
             this.cbEnableStaticCacheByImage.Checked.ToString().Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );

            CommonHelper.SetCookie( "IsBustImageByCss",
                this.cbEnableBustImageByCss.Checked.ToString().Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );
            if ( this.cbEnableBustImageByCss.Checked ) {
                JsAndCssManager.Execute( Server.MapPath( "~/_assets" ) );
            }

            CommonHelper.SetCookie( "IsImageBase64String",
                this.cbEnabelImageBase64String.Checked.ToString().Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );

            // Note Bug
            CommonHelper.SetCookie( "IsSpriteImageOptimizationModule",
        this.cbSpriteImageOptimizationModule.Checked.ToString().Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );
            if ( !this.cbSpriteImageOptimizationModule.Checked ) {
                //ImageManager.RemoveSpriteImageOptimizationModule();
            }
        }
    }
}